package com.yiqi.news.app.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import com.yiqi.news.app.utils.SPUtil;
import com.yiqi.news.app.utils.TimeUtils;


import static com.yiqi.news.app.utils.SPUtil.SP_NAME;

public class UploadTimeOnlineService extends Service {

    private long intervalTime = 1000 * 60;//间隔时间

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        long todayTime = SPUtil.getLong(this, SP_NAME, String.valueOf(TimeUtils.getTodayStartTime()), 0);

//        if (intervalTime > todayTime) {
//            intervalTime -= todayTime;
//        }

        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        long triggerAtTime = SystemClock.elapsedRealtime() + intervalTime;
        Log.d("TAG", "打印时间: " + triggerAtTime);
        Intent i = new Intent("UPLOADTIMEONLINE.ACTION");
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
        return super.onStartCommand(intent, flags, startId);
    }
}
